package com.appointment.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.appointment.dao.ConfirmDao;
import com.appointment.dao.ScoresDao;
import com.appointment.entity.Confirm;
import com.appointment.entity.Score;
import com.appointment.web.PrintTimeFunction;

@Controller
public class SearchController {
	
	@Resource
	ConfirmDao confirmdao;
	
	@Resource
	ScoresDao scoresdao;
	
	@Transactional(rollbackFor={Exception.class})
	@RequestMapping("/searchMessage")
	public String serchMessage(String type, String stu_number, Model model){
		//type==1,证明
		if(type==null){
			return "redirect:/error";
		}
		if(stu_number==null){
			return "redirect:/error";
		}
		if(type.equals("1")){
			Confirm[] c = confirmdao.findConfirmByStuNumber(stu_number);
			if(c.length!=0){
				for(Confirm c1 :c){
					c1.setDay(PrintTimeFunction.getNiceDate(c1.getTime()));
				}
				model.addAttribute("message", c);
			}else{
				model.addAttribute("error", "没有找到信息");
			}
		}else if(type.equals("2")){
			Score[] s = scoresdao.findScoresByStuNumber(stu_number);
			if(s.length!=0){
				for(Score s1 :s){
					s1.setDay(PrintTimeFunction.getNiceDate(s1.getTime()));
				}
				model.addAttribute("message", s);
			}else{
				model.addAttribute("error", "没有找到信息");
			}
		}else{
			return "redirect:/error";
		}
		
		return "require";
	}
	
	@RequestMapping("/require")
	public String require(){
		return "require";
	}
}
